clear all
set more off

*Defining the working folder

global main "insert your directory"

cd "$main"

//////////////////////////////////////////////
//---------------Data construction------------
//////////////////////////////////////////////

*The files named "chl_YYYY" correspond to the data of the Latinobarómetro survey of the year YYYY that consider only the data from Chile.

*All databases are available on the latinobarometro corporation website (https://www.latinobarometro.org/).

//--------------
// Survey 1995
//--------------

clear

*Load 1995 database

use "chl_1995"

*Government approval

generate approval = .
replace approval = 1 if p33==152001 | p33==152002 | p33==152003 | p33==152004 
replace approval = 0 if p33==152005 | p33==152006 | p33==152007 | p33==152010 | p33==152011 |  p33==152024 |  p33==94 |  p33==96

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p3==3
replace  socio_prosp = 1 if p3==2
replace  socio_prosp = 2 if p3==1

generate socio_current = .
replace  socio_current = 0 if p1==4 | p1==5
replace  socio_current = 1 if p1==3
replace  socio_current = 2 if p1==1 | p1==2


*Egotropic variables

generate ego_prosp = .
replace  ego_prosp = 0 if p6==3
replace  ego_prosp = 1 if p6==2
replace  ego_prosp = 2 if p6==1 

generate ego_current = .
replace  ego_current = 0 if p4==4 | p4==5
replace  ego_current = 1 if p4==3
replace  ego_current = 2 if p4==1 | p4==2


*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p27j==1 | p27j==2
replace conf_pp=0 if p27j==3 | p27j==4


*Age

generate age_group="."
replace  age_group="18-24" if s2<25
replace  age_group="25-34" if s2>=25 & s2<35
replace  age_group="35-54" if s2>=35 & s2<55
replace  age_group="55+" if s2>=55
drop if age_group=="."

*Generate indicators for age groups

tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if s20==1 // No education
replace educ_group=2 if s20==2| s20==3 // Primary education
replace educ_group=3 if s20==4| s20==5 // Secondary education
replace educ_group=4 if s20==6| s20==7 // Tertiary education

*Year

gen year = 1995

* Gender

gen gender=s1

* Rename political identification question

rename p31 pol_iden

*Save 1995 database

save "lb1995", replace


//--------------
// Survey 1996
//--------------

clear 

*Load 1996 database

use "chl_1996"

*Government approval

generate approval = .
replace approval = 1 if p40==152001 | p40==152002 | p40==152003 | p40==152004 
replace approval = 0 if p40==152005 | p40==152006 | p40==152007 | p40==152010 | p40==152011 |  p40==94 | p40==96

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p3==3
replace  socio_prosp = 1 if p3==2
replace  socio_prosp = 2 if p3==1

generate socio_current = .
replace  socio_current = 0 if p1==4 | p1==5
replace  socio_current = 1 if p1==3
replace  socio_current = 2 if p1==1 | p1==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if p6==3
replace  ego_prosp = 1 if p6==2
replace  ego_prosp = 2 if p6==1 

generate ego_current = .
replace  ego_current = 0 if p4==4 | p4==5
replace  ego_current = 1 if p4==3
replace  ego_current = 2 if p4==1 | p4==2


*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p33j==1 | p33j==2
replace conf_pp=0 if p33j==3 | p33j==4


*Age

generate age_group="."
replace  age_group="18-24" if s2<25
replace  age_group="25-34" if s2>=25 & s2<35
replace  age_group="35-54" if s2>=35 & s2<55
replace  age_group="55+" if s2>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if s16a==1 // No education
replace educ_group=2 if s16a==2| s16a==3 // Primary education
replace educ_group=3 if s16a==4| s16a==5 // Secondary education
replace educ_group=4 if s16a==6| s16a==7 // Tertiary education

*Year

gen year = 1996

* Gender

gen gender=s1


*Rename political identification question

rename p38 pol_iden

* Region

drop region

gen region=.
replace region=152000 if ciu==152000045 //Metropolitana, RM
replace region=152001 if ciu==152001003 //Tarapacá-Iquique, II
replace region=152002 if ciu==152002001 | ciu==152002002 // Antofagasta-Antofagasta y Antofagasta-Calama, Antofagasta	
replace region=152003 if ciu==152003002 // Atacama-Copiapo, Atacama
replace region=152004 if ciu==152004002 |ciu==152004005|ciu==152004007 //Coq-Coq y Coq-La Serena y Coq-Ovalle
replace region=152005 if ciu==152005022|ciu==152005023|ciu==152005026|ciu==152005031|ciu==152005032|ciu==152005033 // Todo Valpo.
replace region=152006 if ciu==152006019
replace region=152007 if ciu==152007005|ciu==152007009|ciu==152007023 //Maule.
replace region=152008 if ciu==152008006|ciu==152008008|ciu==152008009|ciu==152008018|ciu==152008019|ciu==152008031 //Biobio
replace region=152009 if ciu==152009014 // La araucanía
replace region=152010 if ciu==152010009  | ciu==152010012 // Los lagos.
replace region=152014 if ciu==152014003 // Los ríos
replace region=152015 if ciu==152015001 //Arica y parinacota


*Save 1996 database

save "lb1996", replace



//--------------
// 1997 Survey
//--------------

clear 

*Load 1997 database

use "chl_1997"

*Government approval

generate approval = .
replace approval = 1 if sp58==152001 | sp58==152002 | sp58==152003 
replace approval = 0 if sp58==152005 | sp58==152006 | sp58==152007 | sp58==152010 | sp58==152011 |  sp58==94

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if sp3==3
replace  socio_prosp = 1 if sp3==2
replace  socio_prosp = 2 if sp3==1

generate socio_current = .
replace  socio_current = 0 if sp1==4 | sp1==5
replace  socio_current = 1 if sp1==3
replace  socio_current = 2 if sp1==1 | sp1==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if sp6==3
replace  ego_prosp = 1 if sp6==2
replace  ego_prosp = 2 if sp6==1 

generate ego_current = .
replace  ego_current = 0 if sp4==4 | sp4==5
replace  ego_current = 1 if sp4==3
replace  ego_current = 2 if sp4==1 | sp4==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if sp63g==1 | sp63g==2
replace conf_pp=0 if sp63g==3 | sp63g==4


*Age

generate age_group="."
replace  age_group="18-24" if s2<25
replace  age_group="25-34" if s2>=25 & s2<35
replace  age_group="35-54" if s2>=35 & s2<55
replace  age_group="55+" if s2>=55

drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if s12a==1 // No education
replace educ_group=2 if s12a==2| s12a==3 //Primary education
replace educ_group=3 if s12a==4| s12a==5 //  Secondary education
replace educ_group=4 if s12a==6| s12a==7 // Tertiary education

*Year

gen year = 1997


* Gender

gen gender=s1

* Rename political identification question

rename sp56 pol_iden

*Region

gen region=.
replace region=152000 if ciu==152000045 //Metropolitana, RM
replace region=152001 if ciu==152001003 //Tarapacá-Iquique, II
replace region=152002 if ciu==152002001 | ciu==152002002 // Antofagasta-Antofagasta y Antofagasta-Calama, Antofagasta	
replace region=152003 if ciu==152003002 // Atacama-Copiapo, Atacama
replace region=152004 if ciu==152004002 |ciu==152004005|ciu==152004007 //Coq-Coq y Coq-La Serena y Coq-Ovalle
replace region=152005 if ciu==152005022|ciu==152005023|ciu==152005026|ciu==152005031|ciu==152005032|ciu==152005033 // Todo Valpo.
replace region=152006 if ciu==152006019
replace region=152007 if ciu==152007005|ciu==152007009|ciu==152007023 //Maule.
replace region=152008 if ciu==152008006|ciu==152008008|ciu==152008009|ciu==152008018|ciu==152008019|ciu==152008031 //Biobio
replace region=152009 if ciu==152009014 // La araucanía
replace region=152010 if ciu==152010009  | ciu==152010012 // Los lagos.
replace region=152014 if ciu==152014003 // Los ríos
replace region=152015 if ciu==152015001 //Arica y parinacota


*Save 1997 database

save "lb1997", replace


//--------------
// 1998 Survey
//--------------

clear 

*Load 1998 database

use "chl_1998"

*Government approval 

generate approval = .
replace approval = 1 if sp53==152001 | sp53==152002 | sp53==152003 | sp53==152004 
replace approval = 0 if sp53==152005 | sp53==152006 | sp53==152007 | sp53==152010 | sp53==152011 |  sp53==94 |  sp53==96

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if sp3==3
replace  socio_prosp = 1 if sp3==2
replace  socio_prosp = 2 if sp3==1

generate socio_current = .
replace  socio_current = 0 if sp1==4 | sp1==5
replace  socio_current = 1 if sp1==3
replace  socio_current = 2 if sp1==1 | sp1==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if sp6==3
replace  ego_prosp = 1 if sp6==2
replace  ego_prosp = 2 if sp6==1 

generate ego_current = .
replace  ego_current = 0 if sp4==4 | sp4==5
replace  ego_current = 1 if sp4==3
replace  ego_current = 2 if sp4==1 | sp4==2

*Confidence in political parties

gen conf_pp=. 
replace conf_pp=1 if sp38g==1 | sp38g==2
replace conf_pp=0 if sp38g==3 | sp38g==4


*Age

generate age_group="."
replace  age_group="18-24" if s2<25
replace  age_group="25-34" if s2>=25 & s2<35
replace  age_group="35-54" if s2>=35 & s2<55
replace  age_group="55+" if s2>=55
drop if age_group=="."

tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if s14a==1 // No education
replace educ_group=2 if s14a==2| s14a==3 // Primary education
replace educ_group=3 if s14a==4| s14a==5 // Secondary education
replace educ_group=4 if s14a==6| s14a==7 // Tertiary education


*Year

gen year = 1998

* Gender

gen gender=s1


*Rename weight variable

rename pondera wt


*Rename political identification variable

rename sp52 pol_iden

* Region

rename reg region

*Save 1998 database

save "lb1998", replace


//--------------
// 2001 Survey
//--------------

clear 

*Load 2001 database

use "chl_2001"

*Government approval

generate approval = .
replace approval = 1 if p55st==152001 | p55st==152002 | p55st==152003 | p55st==152004 
replace approval = 0 if p55st==152005 | p55st==152006 | p55st==152007 | p55st==152010 | p55st==152011 |  p55st==94 |  p55st==95 | p55st==96


*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p3st==4 | p3st==5
replace  socio_prosp = 1 if p3st==3
replace  socio_prosp = 2 if p3st==1 | p3st==2

generate socio_current = .
replace  socio_current = 0 if p1st==4 | p1st==5
replace  socio_current = 1 if p1st==3
replace  socio_current = 2 if p1st==1 | p1st==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if p6st==4 | p6st==5
replace  ego_prosp = 1 if p6st==3
replace  ego_prosp = 2 if p6st==1 | p6st==2

generate ego_current = .
replace  ego_current = 0 if p4st==4 | p4st==5
replace  ego_current = 1 if p4st==3
replace  ego_current = 2 if p4st==1 | p4st==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p61stg==1|p61stg==2
replace conf_pp=0 if p61stg==3|p61stg==4

*Age

generate age_group="."
replace  age_group="18-24" if s2<25
replace  age_group="25-34" if s2>=25 & s2<35
replace  age_group="35-54" if s2>=35 & s2<55
replace  age_group="55+" if s2>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if reeduc1==1 // No education
replace educ_group=2 if reeduc1==2| reeduc1==3 // Primary education
replace educ_group=3 if reeduc1==4| reeduc1==5 // Secondary education
replace educ_group=4 if reeduc1==6| reeduc1==7 // Tertiary education

*Year

gen year = 2001

* Gender

gen gender=s1

*Region

rename reg region

* Rename political identification question

rename p54st pol_iden

*Save 2001 database

save "lb2001", replace

//--------------
// 2002 Survey
//--------------

clear 

*Load 2002 database

use "chl_2002"

*Government approval

generate approval = .
replace approval = 1 if p18no2==1
replace approval = 0 if p18no2==2

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p2stc==4 | p2stc==5
replace  socio_prosp = 1 if p2stc==3
replace  socio_prosp = 2 if p2stc==1 | p2stc==2

generate socio_current = .
replace  socio_current = 0 if p2sta==4 | p2sta==5
replace  socio_current = 1 if p2sta==3
replace  socio_current = 2 if p2sta==1 | p2sta==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if p2stf==4 | p2stf==5
replace  ego_prosp = 1 if p2stf==3
replace  ego_prosp = 2 if p2stf==1 | p2stf==2

generate ego_current = .
replace  ego_current = 0 if p2std==4 | p2std==5
replace  ego_current = 1 if p2std==3
replace  ego_current = 2 if p2std==1 | p2std==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p34stf==1|p34stf==2
replace conf_pp=0 if p34stf==3|p34stf==4


*Age

generate age_group="."
replace  age_group="18-24" if s2<25
replace  age_group="25-34" if s2>=25 & s2<35
replace  age_group="35-54" if s2>=35 & s2<55
replace  age_group="55+" if s2>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if reeduc1==1 // No education
replace educ_group=2 if reeduc1==2| reeduc1==3 // Primary education
replace educ_group=3 if reeduc1==4| reeduc1==5 // Secondary education
replace educ_group=4 if reeduc1==6| reeduc1==7 // Tertiary education


*Year

gen year = 2002

* Gender

gen gender=s1

*region

rename reg region

*Rename political identification question

rename p64st pol_iden

*Save 2002 database

save "lb2002", replace

//--------------
// 2003 Survey
//--------------

clear 

*Load 2003 database

use "chl_2003"

*Government approval

generate approval = .
replace approval = 1 if p24st==1
replace approval = 0 if p24st==2

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p3st==4 | p3st==5
replace  socio_prosp = 1 if p3st==3
replace  socio_prosp = 2 if p3st==1 | p3st==2

generate socio_current = .
replace  socio_current = 0 if p1st==4 | p1st==5
replace  socio_current = 1 if p1st==3
replace  socio_current = 2 if p1st==1 | p1st==2


*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if p6st==4 | p6st==5
replace  ego_prosp = 1 if p6st==3
replace  ego_prosp = 2 if p6st==1 | p6st==2

generate ego_current = .
replace  ego_current = 0 if p4st==4 | p4st==5
replace  ego_current = 1 if p4st==3
replace  ego_current = 2 if p4st==1 | p4st==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p21std==1|p21std==2
replace conf_pp=0 if p21std==3|p21std==4

*Age

generate age_group="."
replace  age_group="18-24" if s2<25
replace  age_group="25-34" if s2>=25 & s2<35
replace  age_group="35-54" if s2>=35 & s2<55
replace  age_group="55+" if s2>=55

drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if s18==1 // No education
replace educ_group=2 if s18==2| s18==3 // Primary education
replace educ_group=3 if s18==4| s18==5 // Secondary education
replace educ_group=4 if s18==6| s18==7 // Tertiary education

*Year

gen year = 2003

*Gender

gen gender=s1

*Region

rename reg region

*Rename political identification question

rename p60st pol_iden

*Save 2003 database

save "lb2003", replace



//--------------
// 2004 Survey
//--------------

clear 

*Load 2004 database

use "chl_2004"

*Government approval

generate approval = .
replace approval = 1 if p46st==1
replace approval = 0 if p46st==2

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p4st==4 | p4st==5
replace  socio_prosp = 1 if p4st==3
replace  socio_prosp = 2 if p4st==1 | p4st==2

generate socio_current = .
replace  socio_current = 0 if p2st==4 | p2st==5
replace  socio_current = 1 if p2st==3
replace  socio_current = 2 if p2st==1 | p2st==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if p7st==4 | p7st==5
replace  ego_prosp = 1 if p7st==3
replace  ego_prosp = 2 if p7st==1 | p7st==2

generate ego_current = .
replace  ego_current = 0 if p5st==4 | p5st==5
replace  ego_current = 1 if p5st==3
replace  ego_current = 2 if p5st==1 | p5st==2


*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p34std==1|p34std==2
replace conf_pp=0 if p34std==3|p34std==4

*Age

generate age_group="."
replace  age_group="18-24" if s2<25
replace  age_group="25-34" if s2>=25 & s2<35
replace  age_group="35-54" if s2>=35 & s2<55
replace  age_group="55+" if s2>=55

drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if reeduc1==1 // No education
replace educ_group=2 if reeduc1==2| reeduc1==3 // Primary education
replace educ_group=3 if reeduc1==4| reeduc1==5 // Secondary education
replace educ_group=4 if reeduc1==6| reeduc1==7 // Tertiary education

*Year

gen year = 2004

* Gender

gen gender=s1

*Region

rename reg region

* Rename political identification question

rename p87st pol_iden

*Save 2004 database

save "lb2004", replace


//--------------
// 2005 Survey
//--------------

clear 

*Load 2005 database

use "chl_2005"

*Government approval

generate approval = .
replace approval = 1 if p29st==1
replace approval = 0 if p29st==2

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p4st==4 | p4st==5
replace  socio_prosp = 1 if p4st==3
replace  socio_prosp = 2 if p4st==1 | p4st==2

generate socio_current = .
replace  socio_current = 0 if p2st==4 | p2st==5
replace  socio_current = 1 if p2st==3
replace  socio_current = 2 if p2st==1 | p2st==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if p7st==4 | p7st==5
replace  ego_prosp = 1 if p7st==3
replace  ego_prosp = 2 if p7st==1 | p7st==2

generate ego_current = .
replace  ego_current = 0 if p5st==4 | p5st==5
replace  ego_current = 1 if p5st==3
replace  ego_current = 2 if p5st==1 | p5st==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p47stb==1|p47stb==2
replace conf_pp=0 if p47stb==3|p47stb==4

*Age

generate age_group="."
replace  age_group="18-24" if s7<25
replace  age_group="25-34" if s7>=25 & s7<35
replace  age_group="35-54" if s7>=35 & s7<55
replace  age_group="55+" if s7>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if reeduc1==1 // No education
replace educ_group=2 if reeduc1==2| reeduc1==3 // Primary education
replace educ_group=3 if reeduc1==4| reeduc1==5 // Secondary education
replace educ_group=4 if reeduc1==6| reeduc1==7 // Tertiary education


*Year

gen year=2005

* Gender

gen gender=s6

*Region

rename reg region

* Rename political identification question

rename p34st pol_iden

*Save 2005 database

save "lb2005", replace


//--------------
// 2006 Survey
//--------------

clear 

*Load 2006 database

use "chl_2006"

*Government approval

generate approval = .
replace approval = 1 if p43st==1
replace approval = 0 if p43st==2

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p4st==4 | p4st==5
replace  socio_prosp = 1 if p4st==3
replace  socio_prosp = 2 if p4st==1 | p4st==2

generate socio_current = .
replace  socio_current = 0 if p2st==4 | p2st==5
replace  socio_current = 1 if p2st==3
replace  socio_current = 2 if p2st==1 | p2st==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if p7st==4 | p7st==5
replace  ego_prosp = 1 if p7st==3
replace  ego_prosp = 2 if p7st==1 | p7st==2

generate ego_current = .
replace  ego_current = 0 if p5st==4 | p5st==5
replace  ego_current = 1 if p5st==3
replace  ego_current = 2 if p5st==1 | p5st==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p24st_c==1|p24st_c==2
replace conf_pp=0 if p24st_c==3|p24st_c==4

*Age

generate age_group="."
replace  age_group="18-24" if s7<25
replace  age_group="25-34" if s7>=25 & s7<35
replace  age_group="35-54" if s7>=35 & s7<55
replace  age_group="55+" if s7>55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if reeduc1==1 // No education
replace educ_group=2 if reeduc1==2| reeduc1==3 // Primary education
replace educ_group=3 if reeduc1==4| reeduc1==5 // Secondary education
replace educ_group=4 if reeduc1==6| reeduc1==7 // Tertiary education

*Year

gen year = 2006

* Gender


gen gender=s6

*Region

rename reg region

* Rename political identification question

rename p47st pol_iden

*Save 2006 database

save "lb2006", replace


//--------------
// 2008 Survey
//--------------

clear 

*Load 2008 database

use "chl_2008"

*Government approval

generate approval = .
replace approval = 1 if p32st_a==1
replace approval = 0 if p32st_a==2


*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p6st==4 | p6st==5
replace  socio_prosp = 1 if p6st==3
replace  socio_prosp = 2 if p6st==1 | p6st==2

generate socio_current = .
replace  socio_current = 0 if p4st==4 | p4st==5
replace  socio_current = 1 if p4st==3
replace  socio_current = 2 if p4st==1 | p4st==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if p11st==4 | p11st==5
replace  ego_prosp = 1 if p11st==3
replace  ego_prosp = 2 if p11st==1 | p11st==2

generate ego_current = .
replace  ego_current = 0 if p7st==4 | p7st==5
replace  ego_current = 1 if p7st==3
replace  ego_current = 2 if p7st==1 | p7st==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p28st_c==1|p28st_c==2
replace conf_pp=0 if p28st_c==3|p28st_c==4

*Age

generate age_group="."
replace  age_group="18-24" if s9<25
replace  age_group="25-34" if s9>=25 & s9<35
replace  age_group="35-54" if s9>=35 & s9<55
replace  age_group="55+" if s9>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if reeduc1==1 // No education
replace educ_group=2 if reeduc1==2| reeduc1==3 // Primary education
replace educ_group=3 if reeduc1==4| reeduc1==5 // Secondary education
replace educ_group=4 if reeduc1==6| reeduc1==7 // Tertiary education

*Year

gen year = 2008

* Gender

gen gender=s8

*Region

rename reg region

* Rename political identification question

rename p56st pol_iden

*Save 2008 database

save "lb2008", replace


//--------------
// 2009 Survey
//--------------

clear 

*Load 2009 database

use "chl_2009"

*Government approval

generate approval = .
replace approval = 1 if p68st_a==1
replace approval = 0 if p68st_a==2

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if p5st==4 | p5st==5
replace  socio_prosp = 1 if p5st==3
replace  socio_prosp = 2 if p5st==1 | p5st==2

generate socio_current = .
replace  socio_current = 0 if p3st_a==4 | p3st_a==5
replace  socio_current = 1 if p3st_a==3
replace  socio_current = 2 if p3st_a==1 | p3st_a==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if p8st==4 | p8st==5
replace  ego_prosp = 1 if p8st==3
replace  ego_prosp = 2 if p8st==1 | p8st==2

generate ego_current = .
replace  ego_current = 0 if p6st==4 | p6st==5
replace  ego_current = 1 if p6st==3
replace  ego_current = 2 if p6st==1 | p6st==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if p26st_c==1|p26st_c==2
replace conf_pp=0 if p26st_c==3|p26st_c==4

*Age

generate age_group="."
replace  age_group="18-24" if s6<25
replace  age_group="25-34" if s6>=25 & s6<35
replace  age_group="35-54" if s6>=35 & s6<55
replace  age_group="55+" if s6>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if reeduc1==1 // No education
replace educ_group=2 if reeduc1==2| reeduc1==3 // Primary education
replace educ_group=3 if reeduc1==4| reeduc1==5 // Secondary education
replace educ_group=4 if reeduc1==6| reeduc1==7 // Tertiary education

*Year

gen year = 2009

* Gender

gen gender=s5

*Region

rename reg region

*Rename political identification question

rename p69st pol_iden

*Save 2009 database

save "lb2009", replace

//--------------
// 2010 Survey
//--------------

clear 

*Load 2010 database

use "chl_2010"

*Government approval

generate approval = .
replace approval = 1 if P59ST_A==1
replace approval = 0 if P59ST_A==2

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if P5ST_A==4 | P5ST_A==5
replace  socio_prosp = 1 if P5ST_A==3
replace  socio_prosp = 2 if P5ST_A==1 | P5ST_A==2

generate socio_current = .
replace  socio_current = 0 if P3ST_A==4 | P3ST_A==5
replace  socio_current = 1 if P3ST_A==3
replace  socio_current = 2 if P3ST_A==1 | P3ST_A==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if P8ST==4 | P8ST==5
replace  ego_prosp = 1 if P8ST==3
replace  ego_prosp = 2 if P8ST==1 | P8ST==2

generate ego_current = .
replace  ego_current = 0 if P6ST==4 | P6ST==5
replace  ego_current = 1 if P6ST==3
replace  ego_current = 2 if P6ST==1 | P6ST==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if P20ST_C==1|P20ST_C==2
replace conf_pp=0 if P20ST_C==3|P20ST_C==4

*Age

generate age_group="."
replace  age_group="18-24" if S8<25
replace  age_group="25-34" if S8>=25 & S8<35
replace  age_group="35-54" if S8>=35 & S8<55
replace  age_group="55+" if S8>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if REEDUC1==1 // No education
replace educ_group=2 if REEDUC1==2| REEDUC1==3 // Primary education
replace educ_group=3 if REEDUC1==4| REEDUC1==5 // Secondary education
replace educ_group=4 if REEDUC1==6| REEDUC1==7 // Tertiary education

*Year

gen year = 2010

* Gender

gen gender=S7

*Region

gen region=.
forvalues i=1/9{
	replace region=15200`i' if reg==600`i'
}
forvalues i=10/12{
	replace region=1520`i' if reg==60`i'
}
forvalues i=14/15{
	replace region=1520`i' if reg==60`i'
}
replace region=152000 if reg==6013

* Rename political identification question

rename P60ST pol_iden

*Save 2010 database

save "lb2010", replace


//--------------
// 2011 Survey
//--------------

clear 

*Load 2011 database

use "chl_2011"

*Government approval

generate approval = .
replace approval = 1 if P36ST==1
replace approval = 0 if P36ST==2

*Variables sociotropicas

generate socio_prosp = .
replace  socio_prosp = 0 if P5STIC1A==4 | P5STIC1A==5
replace  socio_prosp = 1 if P5STIC1A==3
replace  socio_prosp = 2 if P5STIC1A==1 | P5STIC1A==2

generate socio_current = .
replace  socio_current = 0 if P3ST_A==4 | P3ST_A==5
replace  socio_current = 1 if P3ST_A==3
replace  socio_current = 2 if P3ST_A==1 | P3ST_A==2

*Variables egotropicas

generate ego_prosp = .
replace  ego_prosp = 0 if P8STICC4==4 | P8STICC4==5
replace  ego_prosp = 1 if P8STICC4==3
replace  ego_prosp = 2 if P8STICC4==1 | P8STICC4==2

generate ego_current = .
replace  ego_current = 0 if P6ST==4 | P6ST==5
replace  ego_current = 1 if P6ST==3
replace  ego_current = 2 if P6ST==1 | P6ST==2


*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if P22ST_C==1|P22ST_C==2
replace conf_pp=0 if P22ST_C==3|P22ST_C==4


*Age

generate age_group="."
replace  age_group="18-24" if S17<25
replace  age_group="25-34" if S17>=25 & S17<35
replace  age_group="35-54" if S17>=35 & S17<55
replace  age_group="55+" if S17>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if REEDUC1==1 // No education
replace educ_group=2 if REEDUC1==2| REEDUC1==3 // Primary education
replace educ_group=3 if REEDUC1==4| REEDUC1==5 // Secondary education
replace educ_group=4 if REEDUC1==6| REEDUC1==7 // Tertiary education

*Year

gen year = 2011

* Gender

gen gender=S16

*Region

rename reg region

* Rename political identification question

rename P76ST pol_iden

*Save 2011 database

save "lb2011", replace


//--------------
// 2013 Survey
//--------------

clear 

*Load 2013 database

use "chl_2013"

*Government approval

generate approval = .
replace approval = 1 if P38STGBS==1
replace approval = 0 if P38STGBS==2

*Sociotropic variables 

generate socio_prosp = .
replace  socio_prosp = 0 if P5STGBS==4 | P5STGBS==5
replace  socio_prosp = 1 if P5STGBS==3
replace  socio_prosp = 2 if P5STGBS==1 | P5STGBS==2

generate socio_current = .
replace  socio_current = 0 if P3STGBS==4 | P3STGBS==5
replace  socio_current = 1 if P3STGBS==3
replace  socio_current = 2 if P3STGBS==1 | P3STGBS==2

*Egotropic variables

generate ego_prosp = .
replace  ego_prosp = 0 if P8STGBS==4 | P8STGBS==5
replace  ego_prosp = 1 if P8STGBS==3
replace  ego_prosp = 2 if P8STGBS==1 | P8STGBS==2

generate ego_current = .
replace  ego_current = 0 if P6STGBS==4 | P6STGBS==5
replace  ego_current = 1 if P6STGBS==3
replace  ego_current = 2 if P6STGBS==1 | P6STGBS==2


*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if P26TGB_G==1|P26TGB_G==2
replace conf_pp=0 if P26TGB_G==3|P26TGB_G==4

*Age

generate age_group="."
replace  age_group="18-24" if S11<25
replace  age_group="25-34" if S11>=25 & S11<35
replace  age_group="35-54" if S11>=35 & S11<55
replace  age_group="55+" if S11>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if REEDUC_1==1 // No education
replace educ_group=2 if REEDUC_1==2| REEDUC_1==3 // Primary education
replace educ_group=3 if REEDUC_1==4| REEDUC_1==5 // Secondary education
replace educ_group=4 if REEDUC_1==6| REEDUC_1==7 // Tertiary education

*Year

gen year = 2013

* Gender

gen gender=S10

*Region

gen region=.
forvalues i=1/9{
replace region=15200`i' if reg==`i'	
}
forvalues i=10/12{
replace region=1520`i' if reg==`i'	
}
forvalues i=14/15{
replace region=1520`i' if reg==`i'	
}
replace region=152000 if reg==13

* Rename political identification variable

rename P41ST pol_iden

*Save 2013 database

save "lb2013", replace


//--------------
// 2015 Survey
//--------------

clear 

*Load 2015 database

use "chl_2015"

*Government approval

generate approval = .
replace approval = 1 if P48STGBS==1
replace approval = 0 if P48STGBS==2

*Sociotropic variables

generate socio_prosp = .
replace  socio_prosp = 0 if P5STICC1==4 | P5STICC1==5
replace  socio_prosp = 1 if P5STICC1==3
replace  socio_prosp = 2 if P5STICC1==1 | P5STICC1==2

generate socio_current = .
replace  socio_current = 0 if P3STGBS==4 | P3STGBS==5
replace  socio_current = 1 if P3STGBS==3
replace  socio_current = 2 if P3STGBS==1 | P3STGBS==2

*Egotropic variables 

generate ego_prosp = .
replace  ego_prosp = 0 if P8STGBS==4 | P8STGBS==5
replace  ego_prosp = 1 if P8STGBS==3
replace  ego_prosp = 2 if P8STGBS==1 | P8STGBS==2

generate ego_current = .
replace  ego_current = 0 if P6STGBS==4 | P6STGBS==5
replace  ego_current = 1 if P6STGBS==3
replace  ego_current = 2 if P6STGBS==1 | P6STGBS==2

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if P19ST_C==1|P19ST_C==2
replace conf_pp=0 if P19ST_C==3|P19ST_C==4

*Age

generate age_group="."
replace  age_group="18-24" if S13<25
replace  age_group="25-34" if S13>=25 & S13<35
replace  age_group="35-54" if S13>=35 & S13<55
replace  age_group="55+" if S13>=55
drop if age_group=="."
tab age_group, gen(age_g)

*Education level

gen educ_group=.
replace educ_group=1 if REEDUC_1==1 // No education
replace educ_group=2 if REEDUC_1==2| REEDUC_1==3 //  Primary education
replace educ_group=3 if REEDUC_1==4| REEDUC_1==5 // Secondary education
replace educ_group=4 if REEDUC_1==6| REEDUC_1==7 // Tertiary education

*Year

gen year = 2015

* Gender

gen gender=S12

*region

rename reg region

* Rename political identification question

rename P27ST pol_iden

*Save 2015 database

save "lb2015", replace


//--------------------------
// Pooled cross section
//--------------------------

clear

use "lb1995"
append using "lb1996"
append using "lb1997"
append using "lb1998"
append using "lb2001"
append using "lb2002"
append using "lb2003"
append using "lb2004"
append using "lb2005"
append using "lb2006"
append using "lb2008"
append using "lb2009"
append using "lb2010"
append using "lb2011"
append using "lb2013"
append using "lb2015"

*Save pooled cross section

save "chl_1995-2015", replace


//-------------------------------------
// Variables: creation and modification
//--------------------------------------

clear all

* Load pooled cross section database

use "chl_1995-2015"

*Political identification

gen political_identification = .
replace political_identification = 1 if (pol_iden>=0 & pol_iden<=5) & year<2010
replace political_identification = 0 if (pol_iden>=6 & pol_iden<=10) & year<2010
replace political_identification = 1 if (pol_iden>=5 & pol_iden<=10) & (year>=2010 & year<=2013)
replace political_identification = 0 if (pol_iden>=0 & pol_iden<=4) & (year>=2010 & year<=2013)
replace political_identification = 1 if (pol_iden>=0 & pol_iden<=5) & (year>=2014 & year<2018)
replace political_identification = 0 if (pol_iden>=6 & pol_iden<=10) & (year>=2014 & year<2018)

*Woman

gen woman = (gender==2)

*Generate indicators for educational groups

tab educ_group, gen(educ_)

*Year dummies

tab year, gen (year_)

*Regional dummies

tab region, gen(region_)

*Save database
save  "chl_1995-2015", replace

///////////////////////////////////////////////////////////////////
//---------------------Logit regression--------------------------
///////////////////////////////////////////////////////////////////


global ne_soc "woman age_g1 age_g2 age_g3 age_g4 educ_1 educ_2 educ_3 educ_4"
global ne_pol "conf_pp political_identification"
global xyear "year_1 year_2  year_3 year_4 year_5 year_6 year_7 year_8 year_9 year_10 year_11 year_12 year_13 year_14 year_15 year_16"
global xregion "region_1 region_2 region_3 region_4 region_5 region_6 region_7 region_8 region_9 region_10 region_11 region_12 region_13 region_14 region_15"


//----------------
// Table 3 results
//----------------

*Column 1

logit approval ego_current socio_current $ne_soc [pw=wt]
margins, dydx(ego_current socio_current $ne_soc) post
outreg2 using "table3", lab word stats(coef se) replace keep(ego_current socio_current $ne_soc) addtext(Region dummies, No, Year dummies, No, Robust SE, No) dec(2)

*Column 2

logit approval ego_current socio_current $ne_soc $ne_pol [pw=wt]
margins, dydx(ego_current socio_current $ne_soc $ne_pol) post
outreg2 using "table3", lab word stats(coef se) append keep(ego_current socio_current $ne_soc $ne_pol) addtext(Region dummies, No, Year dummies, No, Robust SE, No) dec(2)

*Column 3

logit approval ego_current socio_current $ne_soc $ne_pol $xyear region_* [pw=wt]
margins, dydx(ego_current socio_current $ne_soc $ne_pol) post
outreg2 using "table3", lab word stats(coef se) append keep(ego_current socio_current $ne_soc $ne_pol) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, No) dec(2)

*Column 4

logit approval ego_current socio_current $ne_soc $ne_pol $xyear region_* [pw=wt], r
margins, dydx(ego_current socio_current $ne_soc $ne_pol) post
outreg2 using "table3", lab word stats(coef se) append keep(ego_current socio_current $ne_soc $ne_pol) sortvar(ego_current socio_current $ne_soc $ne_pol) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, Yes) dec(2)


//----------------
// Table 4 results
//----------------

*Column 1

logit approval ego_prosp socio_prosp $ne_soc [pw=wt]
margins, dydx(ego_prosp socio_prosp $ne_soc) post
outreg2 using "table4", lab word stats(coef se) replace keep(ego_prosp socio_prosp $ne_soc) addtext(Region dummies, No, Year dummies, No, Robust SE, No) dec(2)

*Column 2

logit approval ego_prosp socio_prosp $ne_soc $ne_pol [pw=wt]
margins, dydx(ego_prosp socio_prosp $ne_soc $ne_pol) post
outreg2 using "table4", lab word stats(coef se) append keep(ego_prosp socio_prosp $ne_soc $ne_pol) addtext(Region dummies, No, Year dummies, No, Robust SE, No) dec(2)

*Column 3

logit approval ego_prosp socio_prosp $ne_soc $ne_pol $xyear region_* [pw=wt]
margins, dydx(ego_prosp socio_prosp $ne_soc $ne_pol) post
outreg2 using "table4", lab word stats(coef se) append keep(ego_prosp socio_prosp $ne_soc $ne_pol) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, No) dec(2)

*Column 4

logit approval ego_prosp socio_prosp $ne_soc $ne_pol $xyear region_* [pw=wt], r
margins, dydx(ego_prosp socio_prosp $ne_soc $ne_pol) post
outreg2 using "table4", lab word stats(coef se) append keep(ego_prosp socio_prosp $ne_soc $ne_pol) sortvar(ego_prosp socio_prosp $ne_soc $ne_pol) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, Yes) dec(2)

//----------------
// Figure 3
//----------------
*ssc install schemepack, replace
set scheme white_piyg

logit approval ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol $xyear region_* [pw=wt], r
margins, dydx(ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol) post
coefplot, horizontal keep(ego_prosp socio_prosp ego_current socio_current) xline(0) ciopts(recast(rcap)) coeflabel(ego_prosp="Prospective Egotropic" socio_prosp="Prospective Sociotropic" ego_current="Current Egotropic" socio_current="Current Sociotropic")


//-----------------
// Table A1 results
//-----------------

*Column 1

logit approval ego_prosp socio_prosp ego_current socio_current $ne_soc [pw=wt]
margins, dydx(ego_prosp socio_prosp ego_current socio_current $ne_soc) post
outreg2 using "tableA1", lab word stats(coef se) replace keep(ego_prosp socio_prosp ego_current socio_current $ne_soc) addtext(Region dummies, No, Year dummies, No, Robust SE, No) dec(2)

*Column 2

logit approval ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol [pw=wt]
margins, dydx(ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol) post
outreg2 using "tableA1", lab word stats(coef se) append keep(ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol) addtext(Region dummies, No, Year dummies, No, Robust SE, No) dec(2)

*Column 3

logit approval ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol $xyear region_* [pw=wt]
margins, dydx(ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol) post
outreg2 using "tableA1", lab word stats(coef se) append keep(ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, No) dec(2)

*Column 4

logit approval ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol $xyear region_* [pw=wt], r
margins, dydx(ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol) post
outreg2 using "tableA1", lab word stats(coef se) append keep(ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol) sortvar(ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, Yes) dec(2)



//----------------
// Figure 4
//----------------


* Year interactions

forvalues i=1/16{
	gen sc_year`i'=socio_current*year_`i'
	gen sp_year`i'=socio_prosp*year_`i'
	gen ec_year`i'=ego_current*year_`i'
	gen ep_year`i'=ego_prosp*year_`i'
}

*Panel A

logit approval ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol $xyear region_* sc_year1-ep_year16 [pw=wt], r
margins, dydx(ec_*) post
coefplot, vertical keep(ec_*) yline(0) ciopts(recast(rcap)) coeflabel(ec_year1="1995" ec_year2="1996" ec_year3="1997" ec_year4="1998" ec_year5="2001" ec_year6="2002" ec_year7="2003" ec_year8="2004" ec_year9="2005" ec_year10="2006" ec_year11="2008" ec_year12="2009" ec_year13="2010" ec_year14="2011" ec_year15="2013" ec_year16="2015")

*Panel B

logit approval ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol $xyear region_* sc_year1-ep_year16 [pw=wt], r
margins, dydx(ep_*) post
coefplot, vertical keep(ep_*) yline(0) ciopts(recast(rcap)) coeflabel(ep_year1="1995" ep_year2="1996" ep_year3="1997" ep_year4="1998" ep_year5="2001" ep_year6="2002" ep_year7="2003" ep_year8="2004" ep_year9="2005" ep_year10="2006" ep_year11="2008" ep_year12="2009" ep_year13="2010" ep_year14="2011" ep_year15="2013" ep_year16="2015")

*Panel C

logit approval ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol $xyear region_* sc_year1-ep_year16 [pw=wt], r
margins, dydx(sc_*) post
coefplot, vertical keep(sc_*) yline(0) ciopts(recast(rcap)) coeflabel(sc_year1="1995" sc_year2="1996" sc_year3="1997" sc_year4="1998" sc_year5="2001" sc_year6="2002" sc_year7="2003" sc_year8="2004" sc_year9="2005" sc_year10="2006" sc_year11="2008" sc_year12="2009" sc_year13="2010" sc_year14="2011" sc_year15="2013" sc_year16="2015")

*Panel D

logit approval ego_prosp socio_prosp ego_current socio_current $ne_soc $ne_pol $xyear region_* sc_year1-ep_year16 [pw=wt], r
margins, dydx(sp_*) post
coefplot, vertical keep(sp_year*) yline(0) ciopts(recast(rcap)) coeflabel(sp_year1="1995" sp_year2="1996" sp_year3="1997" sp_year4="1998" sp_year5="2001" sp_year6="2002" sp_year7="2003" sp_year8="2004" sp_year9="2005" sp_year10="2006" sp_year11="2008" sp_year12="2009" sp_year13="2010" sp_year14="2011" sp_year15="2013" sp_year16="2015")


//----------------
// Table 1
//----------------

*First column

collapse (mean) approval [pw=wt], by(year ego_current)
keep if (year==1995|year==2001|year==2005|year==2010|year==2015)
drop if ego_current==. | approval==.

export excel using "Table1_c1", replace firstrow(variables)

*Second column
use "chl_1995-2015", clear

collapse (mean) approval [pw=wt], by(year socio_current)

keep if (year==1995|year==2001|year==2005|year==2010|year==2015)
drop if socio_current==. | approval==.

export excel using "Table1_c2", replace firstrow(variables)


//----------------
// Table 2
//----------------

use "chl_1995-2015", clear

putexcel set "Table 2", replace
putexcel A1=""
putexcel B1="Mean"
putexcel C1="Standard deviation"
putexcel D1="Minimum"
putexcel E1="Max"

local i=2
foreach j in approval socio_current socio_prosp ego_current ego_prosp political_identification  conf_pp{
	sum `j'
	putexcel A`i'="`j'"
	putexcel B`i'=matrix(r(mean))
	putexcel C`i'=matrix(r(sd))
	putexcel D`i'=matrix(r(min))
	putexcel E`i'=matrix(r(max))
local ++i
}

sum pol_iden if pol_iden>=0

putexcel A9="pol_iden"
putexcel B9=matrix(r(mean))
putexcel C9=matrix(r(sd))
putexcel D9=matrix(r(min))
putexcel E9=matrix(r(max))


 